package game.logic.block;

import java.util.Iterator;

import game.logic.BlockPoint;
import game.util.ColorAdapter;
import game.util.Game;

public abstract class Block implements Iterable<BlockPoint>{
	
	private BlockPoint root;
	protected BlockPoint [] dis;
	protected ColorAdapter color;
	
	
	protected int [] dis_x;
	protected int [] dis_y;
	abstract protected void setDis();
	abstract protected void setColor();
	
	
	@Override 
	public Iterator<BlockPoint> iterator() {
		return new BlockIt();
	};
	
	public Block() {
		root = new BlockPoint(0, 0);
		setDis();
		setColor();
		dis = new BlockPoint[dis_x.length];
		for(int i =0 ; i < dis.length;i++)
		{
			dis[i] = new BlockPoint(dis_x[i], dis_y[i]);
			dis[i].color = color;
		}
		
		
	}

	public ColorAdapter getColor(){
		return color;
	}

	public int getRoot_x() {
		return root.x;
	}

	public void setRoot_x(int root_x) {
		this.root.x = root_x;
	}

	public int getRoot_y() {
		return root.y;
	}

	public void setRoot_y(int root_y) {
		this.root.y = root_y;
	}
	
	public void down()
	{
		root.y = root.y + 1;
	}
	
	public void up()
	{
		root.y = root.y - 1;
	}
	
	public void left()
	{
		root.x=root.x-1;
	}
	
	public void right()
	{
		root.x=root.x+1;
	}
	
	public boolean hit(){
		for(int i=0;i<dis.length;i++)
		{
			if(dis[i].y + root.y >=  Game.ROWNUM || dis[i].x + root.x >= Game.COLNUM || dis[i].y + root.y <0 || dis[i].x + root.x  < 0)
			{
				return true;
			}
		}
		return false;
	}
	
	public boolean hit(int x,int y)
	{	
		for(int i=0;i<dis.length;i++)
		{
			System.out.println("Point:" + x + "," + y);
			if(dis[i].y + root.y == y && dis[i].x + root.x == x)
			{
				return true;
			}	
		}
		return hit();
	}
	
	public boolean inArea()
	{	
		for(int i=0;i<dis.length;i++)
		{
			if(dis[i].y + root.y <0 || dis[i].x + root.x <0  || dis[i].y + root.y >= Game.COLNUM || dis[i].x + root.x >= Game.ROWNUM)
			{
				return false;
			}
		}
		return true;
	}
	
	public void rotate()
	{
		for(int i=0;i<dis.length;i++)
		{
			int temp = dis[i].x;
			dis[i].x = dis[i].y - 1;
			dis[i].y = 1 - temp;
		}
	}
	public void cancelRotate()
	{
		for(int i=0;i<dis.length;i++)
		{
			int temp = dis[i].x;
			dis[i].x = 1 - dis[i].y;
			dis[i].y = 1 + temp;
		}
	}
	
	class BlockIt implements Iterator<BlockPoint>{

		int i = 0;
		
		@Override
		public boolean hasNext() {
			if(i < dis.length)
			{
				return true;
			}
			return false;
		}

		@Override
		public BlockPoint next() {
			if(i < dis.length){
				BlockPoint blockPoint = new BlockPoint(dis[i].x + root.x, dis[i].y+ root.y);
				blockPoint.color = dis[i].color;
				i++;
				return blockPoint;
			}
			return null;
		}
		
	}

}
